package com.lry.basic.algorithm.trie;

import org.junit.jupiter.api.Test;

import java.util.TreeSet;

public class Main {

    public static void main(String[] args) {
        Trie trie = new Trie();
        trie.add("hello",1);
        trie.add("helloooo");
        System.out.println(trie.match("he..o"));
        trie.add("cat",1);
        trie.add("dog",1);
        trie.add("deer",1);
        trie.add("pan",1);
        trie.add("panda",1);

        System.out.println(trie.getWords());
        System.out.println(trie.getWeight("hello"));
//        System.out.println(trie.getWeight("hel"));

        System.out.println(trie.sumWeight("hel"));

        trie.delete("pan");
        //trie.delete("panda");
        System.out.println(trie.getWords());

    }

    @Test
    public void testDelete(){
        System.out.println("Pride and Prejudice");
        java.util.Set<String> words = new TreeSet<>();
        if(FileOperation.readFile("pride-and-prejudice.txt", words)){
            Trie trie = new Trie();
            for(String word: words)
                trie.add(word);

            for(String word:words){
                int beforeSize = trie.size();
                trie.delete(word);
                int afterSize = trie.size();
                if(afterSize!=beforeSize-1){
                    System.err.println("删除"+word+"失败");
                }
                if(trie.contains(word)){
                    System.err.println("删除"+word+"失败");
                }
                if(afterSize<=10){
                    System.out.println("\nword="+word);
                    System.out.println("\n"+trie.getWords());
                }
            }

        }
    }

    @Test
    public void testSpeed() {

        System.out.println("Pride and Prejudice");

        java.util.Set<String> words = new TreeSet<>();
        if(FileOperation.readFile("pride-and-prejudice.txt", words)){

            long startTime = System.nanoTime();

            BST<String> set = new BST<>();
            for(String word: words)
                set.add(word);

            for(String word: words)
                set.contains(word);

            long endTime = System.nanoTime();

            double time = (endTime - startTime) / 1000000000.0;

            System.out.println("Total different words: " + set.size());
            System.out.println("BSTSet: " + time + " s");

            // ---

            startTime = System.nanoTime();

            Trie trie = new Trie();
            for(String word: words)
                trie.add(word,1);

            for(String word: words)
                trie.contains(word);

            endTime = System.nanoTime();

            time = (endTime - startTime) / 1000000000.0;

            System.out.println("Total different words: " + trie.size());
            System.out.println("Trie: " + time + " s");
        }
    }

}
